package com.iotechn.unimall.admin.api.treeDetails;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.TreeDetailsDO;
import com.iotechn.unimall.data.mapper.TreeDetailsMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Service
public class AdminTreeDetailsServiceImpl implements AdminTreeDetailsService {
    @Autowired
    private TreeDetailsMapper treeDetailsMapper;

    @Override
    public Page<TreeDetailsDO> list(Integer page, Integer limit, String treeDetailNo, Integer treeId, Long adminId) throws ServiceException {
        List<TreeDetailsDO> TreeDetailsDO = treeDetailsMapper.selectByList(treeDetailNo, treeId, (page - 1) * limit, limit);
        Integer count = treeDetailsMapper.selectCount(new EntityWrapper<>());
        return new Page<>(TreeDetailsDO, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TreeDetailsDO create(TreeDetailsDO treeDetailsDO, Long adminId) throws ServiceException {
        Wrapper<TreeDetailsDO> wrapper = new EntityWrapper<TreeDetailsDO>()
                .eq("tree_detail_no", treeDetailsDO.getTreeDetailNo())
                .eq("tree_id", treeDetailsDO.getTreeId());
        if (treeDetailsMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_LAND_CANNOT_REPEATED);
        if (treeDetailsMapper.insert(treeDetailsDO) > 0) {
            treeDetailsDO.setId(treeDetailsDO.getId());
            return treeDetailsDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(TreeDetailsDO treeDetailsDO, Long adminId) throws ServiceException {
        Wrapper<TreeDetailsDO> wrapper = new EntityWrapper<TreeDetailsDO>()
                .eq("tree_detail_no", treeDetailsDO.getTreeDetailNo())
                .eq("tree_id", treeDetailsDO.getTreeId())
                .ne("id", treeDetailsDO.getId());
        if (treeDetailsMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.TREE_LAND_CANNOT_REPEATED);
        Long id = treeDetailsDO.getId();
        if (id == null) return "fail";
        if (treeDetailsMapper.updateById(treeDetailsDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (treeDetailsMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public TreeDetailsDO detail(Long id, Long adminId) throws ServiceException {
        return treeDetailsMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        if (treeDetailsMapper.delete(new EntityWrapper<TreeDetailsDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_NOT_EXIST);
        return "ok";
    }
}
